<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="题库名称" prop="name">
                <el-input
                        v-model="queryParams.name"
                        placeholder="请输入题库名称"
                        clearable
                        @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="显示顺序" prop="sortNum">
                <el-input
                        v-model="queryParams.sortNum"
                        placeholder="请输入显示顺序"
                        clearable
                        @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        plain
                        icon="Plus"
                        @click="handleAdd"
                        v-hasPermi="['business:QuestionBank:add']"
                >新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="success"
                        plain
                        icon="Edit"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['business:QuestionBank:edit']"
                >修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="danger"
                        plain
                        icon="Delete"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['business:QuestionBank:remove']"
                >删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="warning"
                        plain
                        icon="Download"
                        @click="handleExport"
                        v-hasPermi="['business:QuestionBank:export']"
                >导出
                </el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="QuestionBankList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="题库编号" align="center" prop="questionBankId"/>
            <el-table-column label="题库名称" align="center" prop="name"/>
            <el-table-column label="logo" align="center">
                <template #default="{row}">
                    <image-preview v-if="row.fileList && row.fileList.length>0" :width="50" :height="50"
                                   :src="row.fileList[0].filePath"/>
                    <el-image v-else style="width: 50px; height: 50px"/>
                </template>
            </el-table-column>
            <el-table-column label="显示顺序" align="center" prop="sortNum"/>
            <el-table-column label="创建时间" align="center" prop="createTime" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                               v-hasPermi="['business:QuestionBank:edit']">修改
                    </el-button>
                    <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)"
                               v-hasPermi="['business:QuestionBank:remove']">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改题库对话框 -->
        <el-dialog :title="title" v-model="open" width="80%" append-to-body>
            <el-form ref="QuestionBankRef" :model="form" :rules="rules" label-width="80px">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="题库名称" prop="name">
                            <el-input v-model="form.name" placeholder="请输入题库名称"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="显示顺序" prop="sortNum">
                            <el-input-number :min="0" :max="255" class="ff" v-model="form.sortNum"
                                             placeholder="请输入显示顺序"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="logo图片" prop="fileList">
                    <image-upload :biz-type="FileBizTypeEnum.QUESTION_BANK_ICON" :limit="1"
                                  v-model="form.fileList"/>
                </el-form-item>
                <el-form-item label="题目列表">
                    <el-transfer
                            class="ff"
                            v-model="form.questionIds"
                            filterable
                            filter-placeholder="搜索"
                            :filter-method="filterMethod"
                            :data="questionList"
                            :titles="['未选择的题目','已选择的题目']"
                    />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="QuestionBank">
import {
    listQuestionBank,
    getQuestionBank,
    delQuestionBank,
    addQuestionBank,
    updateQuestionBank
} from "@/api/business/QuestionBank";
import FileBizTypeEnum from "@/enum/FileBizTypeEnum.js";
import ImageUpload from "@/components/ImageUpload/index.vue";
import ImagePreview from "@/components/ImagePreview/index.vue";
import {allListQuestion} from "@/api/business/Question.js";

const {proxy} = getCurrentInstance();

const QuestionBankList = ref([]);
const questionList = ref([])
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        sortNum: null,
    },
    rules: {
        name: [
            {required: true, message: "题库名称不能为空", trigger: "blur"}
        ],
        sortNum: [
            {required: true, message: "显示顺序不能为空", trigger: "blur"}
        ],
        fileList: [
            {required: true, message: "请上传logo图片", trigger: "blur"}
        ],
    }
});

const {queryParams, form, rules} = toRefs(data);

function getQuestionList() {
    allListQuestion().then(res => {
        questionList.value = res.data.map(v => {
            return {
                key: v.questionId,
                label: v.title,
                search: `${v.questionCategoryId}-${v.title}`
            }
        })
    })
}

/** 查询题库列表 */
function getList() {
    loading.value = true;
    listQuestionBank(queryParams.value).then(response => {
        QuestionBankList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

const filterMethod = (query, item) => {
    return item?.search?.toLowerCase()?.includes(query?.toLowerCase());
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 表单重置
function reset() {
    form.value = {
        questionBankId: null,
        name: null,
        sortNum: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        fileList: [],
        questionIds: []
    };
    proxy.resetForm("QuestionBankRef");
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.questionBankId);
    single.value = selection.length !== 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加题库";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const _questionBankId = row.questionBankId || ids.value
    getQuestionBank(_questionBankId).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改题库";
    });
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["QuestionBankRef"].validate(valid => {
        if (valid) {
            if (form.value.questionBankId != null) {
                updateQuestionBank(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                addQuestionBank(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _questionBankIds = row.questionBankId || ids.value;
    proxy.$modal.confirm('是否确认删除题库编号为"' + _questionBankIds + '"的数据项？').then(function () {
        return delQuestionBank(_questionBankIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {
    });
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download('business/QuestionBank/export', {
        ...queryParams.value
    }, `QuestionBank_${new Date().getTime()}.xlsx`)
}

getQuestionList();
getList();
</script>

<style scoped lang="scss">
:deep(.el-transfer-panel) {
  width: 40%;
}
</style>
